package com.tianji.learning.listener;

import com.tianji.api.dto.leanring.SignInMessage;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.JsonUtils;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.service.IPointsRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OnPoingChangeListener {


    @Autowired
    private IPointsRecordService pointService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.Queue.SIGN_POINTS_QUEUE),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.SIGN_IN
    ))
    public void onPointChange(SignInMessage msg){
        log.info("收到了签到积分变更消息:{}", JsonUtils.toJsonStr(msg));
        pointService.addUserPoint(msg.getUserId(), msg.getPoints(), PointsRecordType.SIGN);
    }

}
